package com.ruoyi.college.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.ruoyi.college.domain.vo.TrainingSubjectVo;
import com.ruoyi.trainingManagement.domain.TrainingProgramSubject;
import com.ruoyi.trainingManagement.service.ITrainingProgramSubjectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.college.domain.ResearchDirection;
import com.ruoyi.college.service.IResearchDirectionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学科研究方向Controller
 * 
 * @author ruoyi
 * @date 2025-07-31
 */
@RestController
@RequestMapping("/college/researchDirection")
public class ResearchDirectionController extends BaseController
{
    @Autowired
    private IResearchDirectionService researchDirectionService;
    @Autowired
    private ITrainingProgramSubjectService trainingProgramSubjectService;

    /**
     * 查询学科研究方向列表
     */
   // @PreAuthorize("@ss.hasPermi('college:researchDirection:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResearchDirection researchDirection)
    {
        startPage();
        List<ResearchDirection> list = researchDirectionService.selectResearchDirectionList(researchDirection);
        return getDataTable(list);
    }

    /**
     * 导出学科研究方向列表
     */
    @PreAuthorize("@ss.hasPermi('college:researchDirection:export')")
    @Log(title = "学科研究方向", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchDirection researchDirection)
    {
        List<ResearchDirection> list = researchDirectionService.selectResearchDirectionList(researchDirection);
        ExcelUtil<ResearchDirection> util = new ExcelUtil<ResearchDirection>(ResearchDirection.class);
        util.exportExcel(response, list, "学科研究方向数据");
    }

    /**
     * 获取学科研究方向详细信息
     */
    @PreAuthorize("@ss.hasPermi('college:researchDirection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(researchDirectionService.selectResearchDirectionById(id));
    }

    /**
     * 新增学科研究方向
     */
    @PreAuthorize("@ss.hasPermi('college:researchDirection:add')")
    @Log(title = "学科研究方向", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResearchDirection researchDirection)
    {
        return toAjax(researchDirectionService.insertResearchDirection(researchDirection));
    }

    /**
     * 修改学科研究方向
     */
    @PreAuthorize("@ss.hasPermi('college:researchDirection:edit')")
    @Log(title = "学科研究方向", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResearchDirection researchDirection)
    {
        return toAjax(researchDirectionService.updateResearchDirection(researchDirection));
    }

    /**
     * 删除学科研究方向
     */
    @PreAuthorize("@ss.hasPermi('college:researchDirection:remove')")
    @Log(title = "学科研究方向", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(researchDirectionService.deleteResearchDirectionByIds(ids));
    }
    /**
     *根据院id查询学科研究方向
     */
    @GetMapping("/getResearchDirectionByCollegeId/{collegeId}")
    public AjaxResult getResearchDirectionByCollegeId(@PathVariable String collegeId)
    {
        ResearchDirection researchDirection=new ResearchDirection();
        researchDirection.setCollegeId(collegeId);
        List<ResearchDirection> list = researchDirectionService.selectResearchDirectionList(researchDirection);
        return success(list);
    }

    /**
     * 根据培养方案id获取对应的学科信息（包含一级学科、二级学科、研究方向名称）
     */
    @GetMapping("/getSubjectIdListByProgramId/{programId}")
    public AjaxResult getSubjectIdListByProgramId(@PathVariable String programId) {
        // 1. 根据培养方案ID查询关联的学科列表（TrainingProgramSubject）
        List<TrainingProgramSubject> subjectList = trainingProgramSubjectService.getSubjectsByTrainingProgramId(programId);

        // 2. 调用Service层方法，转换为包含一/二级学科和研究方向的VO列表
        List<TrainingSubjectVo> resultVoList = researchDirectionService.selectSubjectListByProgramId(subjectList);

        // 3. 返回处理后的结果（使用AjaxResult包装）
        return success(resultVoList);
    }

}
